﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using BizModels;
using BizModels.Common;
using IRepositories;
using Repositories;
using ViewModels.BackLog;
using WebLib.AuthorizeService;

namespace HinokyWeb.Controllers
{
    public class BackLogController : Controller
    {
        private IBackLogRepository _backLogRepository;

        public BackLogController()
        {
            _backLogRepository = new BackLogRepository();
        }

        [HttpGet]
        public ActionResult GetBackLogList()
        {
            var dataList = _backLogRepository.GetBackLogList(new BackLogSearchT());

            var backLogListVM = new BackLogListVM();
            foreach (var data in dataList)
            {
                backLogListVM.BackLogList.Add(new BackLogObjVM
                {
                    Idx = data.Idx,
                    State = data.State,
                    Category = data.Category,
                    Type = data.Type,
                    Importance = data.Importance,
                    Title =  data.Title,
                    InsertDate = data.UpdateDate == null ? data.InsertDate : Convert.ToDateTime(data.UpdateDate),
                    UserId = data.CreateUser.UserId,
                    UserKey = data.CreateUser.UserKey,
                    UserName = data.CreateUser.UserName
                });
            }

            foreach (var backlog in backLogListVM.BackLogList)
            {
                if (backlog.State == "3" || backlog.State == "5") continue;

                var timeSpan = DateTime.Now - backlog.InsertDate;
                backlog.DDay = timeSpan.Days == 0 ? "New" : string.Format("+{0}", timeSpan.Days);
            }

            return View(backLogListVM);
        }

        [HttpPost]
        [Authorize(Roles = "ADMIN")]
        public ActionResult SaveBackLog(BackLogObjVM model)
        {
            var backLogT = new BackLogT();
            backLogT.Idx = model.Idx;
            backLogT.State = model.State;
            backLogT.Category = model.Category;
            backLogT.Type = model.Type;
            backLogT.Importance = string.Empty;
            backLogT.Title = model.Title;

            var returnValue = 0;
            if (model.Idx == 0)
            {
                backLogT.InsertDate = DateTime.Now;
                backLogT.CreateUser.UserKey = MemberInfo.Instance.UserKey;   
                returnValue = _backLogRepository.AddBackLog(backLogT);
            }
            else
            {
                backLogT.UpdateDate = DateTime.Now;
                backLogT.UpdateUser.UserKey = MemberInfo.Instance.UserKey;
                returnValue = _backLogRepository.SetBackLog(backLogT);
            }

            return Json(new { Success = returnValue > 0, Idx = returnValue, IsAdded = model.Idx == 0 });
        }

        [HttpPost]
        [Authorize(Roles = "ADMIN")]
        public ActionResult RemoveBackLog(string[] arrIdx)
        {
            if (arrIdx == null || arrIdx.Length == 0) return Json(new { Success = false, ErrorType = "TODO" });

            var list = arrIdx.Select(idx => Convert.ToInt32(idx)).ToList();

            var result = _backLogRepository.RemoveBackLog(list);

            return Json(result ? new { Success = true, ErrorType = "" } : new { Success = false, ErrorType = "시스템 에러가 발생되었습니다." });
        }
    }
}
